package Salesman;

import java.text.DecimalFormat;

public final class ArrayUtils {
        public static double[] ad(double... a) {
        return a;
    }

    public static double[][] aad(double[]... a) {
        return a;
    }

    public static String aad2str(double[][] aad) {
        return aad2str(aad, "0.#", 8);
    }

    public static String aad2str(double[][] aad,String format) {
        return aad2str(aad, format, 8);
    }

    public static String aad2str(double[][] aad,String format, int length) {
        String result = "";
        for (int x = 0; x < aad[0].length; x++) {
            if (x > 0) result += "\n";
            for (int y = 0; y < aad.length; y++) {
                String s = d2str(aad[x][y], format);
                while (s.length() < length) {
                    s = " " + s;
                }
                result += s;
            }
        }
        return result;
    }

    public static String d2str(double d) {
        return new DecimalFormat("0.#").format(d);
    }

    public static String d2str(double d, String format) {
        return new DecimalFormat(format).format(d);
    }

    public static double minOf(double[] da) {
        double d = Double.POSITIVE_INFINITY;
        for(int i = 0; i < da.length; i++) {
            d = Math.min(d, da[i]);
        }
        return d;
    }

    public static double[] colOf(double[][] aad, int c) {
        double[] ad = new double[aad.length];
        for (int i = 0; i < aad.length; i++) {
            ad[i] = aad[i][c];
        }
        return ad;
    }

    public static double[] minus(double[] a1, double[] a2) {
        if (a1.length != a2.length) throw new IllegalArgumentException("arrays must have same length");
        double[] ad = new double[a1.length];
        for (int i = 0; i < ad.length; i++) {
            ad[i] = a1[i] - a2[i];
        }
        return ad;
    }

    public static double sumOf(double[] ad) {
        double d = 0;
        for (int i = 0; i < ad.length; i++) {
            if (!Double.isInfinite(ad[i]) && !Double.isNaN(ad[i])) {
                d += ad[i];
            }
        }
        return d;
    }

    public static double[] concat(double[] a1, double[] a2) {
        double[] ad = new double[a1.length + a2.length];
        System.arraycopy(a1, 0, ad, 0, a1.length);
        System.arraycopy(a2, 0, ad, a1.length, a2.length);
        return ad;
    }

    public static double[][] copyaad(double[][] m) {
        double[][] aad = new double[m.length][m.length];
        for (int i = 0; i < m.length; i++) {
            System.arraycopy(m[i], 0, aad[i], 0, m.length);
        }
        return aad;
    }


    private ArrayUtils() {
    }
}
